﻿using System.Collections;
using System.Collections.Generic;
using Pathfinding;
using UnityEngine;

public class TurnManager : MonoBehaviour
{
    public TurnUI UI;
    public MonsterManager monsterManager;

    public Transform leftScene;
    public Transform rightScene;
    public GameObject Boss;
    public Transform player1;
    public Transform player2;
    public Player2Controller player1controller;
    public Player2Controller player2controller;
    private Transform camera1;
    private Transform camera2;
    public BoxCollider2D leftBound;
    public BoxCollider2D rightBound;
    int cnt = 1;
    float timer;
    float faketimer = 30f;

    void takeTurn(int cnt) {
        if (cnt % 2 == 0)
        {
            player2controller.asset=player1controller.asset;
            player1controller.energy = player2controller.energy;
            player1controller.asset = 0; player2controller.energy = 0;
            //    player1.GetComponent<Player1Controller>().enabled = true;
            //    player2.SendMessage("stop");
            //    player2.GetComponent<Player2Controller>().enabled = false;            
            //monsterManager.changeScene(leftScene, player1);
            monsterManager.changeTarget(player1);
            Boss.GetComponent<AIDestinationSetter>().target = player1;

            Vector3 localPos = player1.localPosition;
            player1.parent = rightScene;
            player1.localPosition = localPos;
            player1.GetComponent<PlayerInput>().mode = 2;
            player1.GetComponent<Player2Controller>().playerPos.parent = leftScene;
            player1.GetComponent<Player2Controller>().playerPos.localPosition = localPos;

            localPos = player2.localPosition;
            player2.parent = leftScene;
            player2.localPosition = localPos;
            player2.GetComponent<PlayerInput>().mode = 1;
            player2.GetComponent<Player2Controller>().playerPos.parent = rightScene;
            player2.GetComponent<Player2Controller>().playerPos.localPosition = localPos;

            camera1.GetComponent<CameraConfiner>().changeBound(rightBound);
            camera2.GetComponent<CameraConfiner>().changeBound(leftBound);
        }
        else {
            player1controller.asset = player2controller.asset;
            player2controller.energy = player1controller.energy;
            player2controller.asset = 0; player1controller.energy = 0;
            //    player1.SendMessage("stop");
            //    player1.GetComponent<Player1Controller>().enabled = false;            
            //    player2.GetComponent<Player2Controller>().enabled = true;
            //monsterManager.changeScene(rightScene, player2);
            monsterManager.changeTarget(player2);
            Boss.GetComponent<AIDestinationSetter>().target = player2;

            Vector3 localPos = player1.localPosition;
            player1.parent = leftScene;
            player1.localPosition = localPos;
            player1.GetComponent<PlayerInput>().mode = 1;
            player1.GetComponent<Player2Controller>().playerPos.parent = rightScene;
            player1.GetComponent<Player2Controller>().playerPos.localPosition = localPos;

            localPos = player2.localPosition;
            player2.parent = rightScene;
            player2.localPosition = localPos;
            player2.GetComponent<PlayerInput>().mode = 2;
            player2.GetComponent<Player2Controller>().playerPos.parent = leftScene;
            player2.GetComponent<Player2Controller>().playerPos.localPosition = localPos;

            camera1.GetComponent<CameraConfiner>().changeBound(leftBound);
            camera2.GetComponent<CameraConfiner>().changeBound(rightBound);
        }
        UI.Swap();
    }

    private void Awake()
    {
        timer = Random.Range(30, 60);
        player1 = leftScene.Find("Player1");
        player2 = rightScene.Find("Player2");


        player1controller = player1.GetComponent<Player2Controller>();
        player2controller = player2.GetComponent<Player2Controller>();
        player1controller.hpshow= leftScene.GetComponentInChildren<HPshow>();
        player2controller.hpshow = rightScene.GetComponentInChildren<HPshow>();

        camera1 = player1.Find("Main Camera");
        camera2 = player2.Find("Main Camera (1)");

        monsterManager = GameObject.Find("MonsterManager").GetComponent<MonsterManager>();
    }

    // Start is called before the first frame update
    void Start()
    {
        cnt++;
    }

    // Update is called once per frame
    void Update()
    {
        if (timer > 0)
        {
            timer -= Time.deltaTime;
            faketimer -= Time.deltaTime;
            TextCountDown.Instance.SetTime(faketimer);
        }
        else {
            timer = Random.Range(30, 60);
            faketimer = 30;
            takeTurn(cnt++);
            Debug.Log("take turn");
        }
    }
}
